package kr.ac.skhu.m.controller;

import java.net.URL;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import kr.ac.skhu.m.data.IndexBoardData;
import kr.ac.skhu.m.data.IndexInfoData;
import kr.ac.skhu.m.data.SkhuNewsData;
import kr.ac.skhu.m.value.InfoValue;
import kr.ac.skhu.m.value.NoticeListValue;

import net.htmlparser.jericho.Element;
import net.htmlparser.jericho.HTMLElementName;
import net.htmlparser.jericho.Source;

import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.mvc.AbstractController;

/**
 * 인덱스 컨트롤러
 * @author han
 *
 */
public class IndexC extends AbstractController {
	
	private String successView;
	
	public void setSuccessView(String successView) {
		this.successView = successView;
	}

	@Override
	protected ModelAndView handleRequestInternal(HttpServletRequest request,
			HttpServletResponse response) throws Exception {
		
		ModelAndView mav = new ModelAndView(successView);
		IndexBoardData boardDataUtil = new IndexBoardData();
		
		/**
		 * 학사공지
		 */
		//학사공지 대학
		boardDataUtil.setUrl("http://www.skhu.ac.kr/board/boardlist.aspx?bsid=10004&searchBun=51");
		List<NoticeListValue> notice_1_1 = boardDataUtil.getNoticeListValue();
		mav.addObject("notice_1_1", notice_1_1);
		
		//학사공지 대학원
		boardDataUtil.setUrl("http://www.skhu.ac.kr/board/boardlist.aspx?bsid=10004&searchBun=52");
		List<NoticeListValue> notice_1_2 = boardDataUtil.getNoticeListValue();
		mav.addObject("notice_1_2", notice_1_2);
		
		/**
		 * 수업공지
		 */
		//수업공지 대학
		boardDataUtil.setUrl("http://www.skhu.ac.kr/board/boardlist.aspx?bsid=10005&searchBun=53");
		List<NoticeListValue> notice_2_1 = boardDataUtil.getNoticeListValue();
		mav.addObject("notice_2_1", notice_2_1);
		
		//수업공지 대학원
		boardDataUtil.setUrl("http://www.skhu.ac.kr/board/boardlist.aspx?bsid=10005&searchBun=54");
		List<NoticeListValue> notice_2_2 = boardDataUtil.getNoticeListValue();
		mav.addObject("notice_2_2", notice_2_2);
		
		/**
		 * 장학공지
		 */
		//장학공지 대학
		boardDataUtil.setUrl("http://www.skhu.ac.kr/board/boardlist.aspx?bsid=10006&searchBun=75");
		List<NoticeListValue> notice_3_1 = boardDataUtil.getNoticeListValue();
		mav.addObject("notice_3_1", notice_3_1);
		
		//장학공지 대학원
		boardDataUtil.setUrl("http://www.skhu.ac.kr/board/boardlist.aspx?bsid=10006&searchBun=76");
		List<NoticeListValue> notice_3_2 = boardDataUtil.getNoticeListValue();
		mav.addObject("notice_3_2", notice_3_2);
		
		/**
		 * 일반공지
		 */
		boardDataUtil.setUrl("http://www.skhu.ac.kr/board/boardlist.aspx?bsid=10007");
		List<NoticeListValue> notice_4 = boardDataUtil.getNoticeListValue();
		mav.addObject("notice_4", notice_4);
		
		/**
		 * 행사공지
		 */
		boardDataUtil.setUrl("http://www.skhu.ac.kr/board/boardlist.aspx?bsid=10008");
		List<NoticeListValue> notice_5 = boardDataUtil.getNoticeListValue();
		mav.addObject("notice_5", notice_5);
		
		
		/**
		 * 자유게시판
		 */
		boardDataUtil.setUrl("http://www.skhu.ac.kr/board/boardlist.aspx?bsid=10017");
		List<NoticeListValue> free = boardDataUtil.getNoticeListValue();
		mav.addObject("free", free);
		
		/**
		 * SKHU_NEWS
		 */
		SkhuNewsData skhuNewsData = new SkhuNewsData("http://www.skhu.ac.kr/board/boardlist.aspx?bsid=10009");
		mav.addObject("skhuNews", skhuNewsData.getNoticeListValue());
		
		/**
		 * 학사안내 날짜 출력
		 */
		Calendar calendar = Calendar.getInstance();
		int year = calendar.get(Calendar.YEAR);
		int month = calendar.get(Calendar.MONTH) + 1;
		
		mav.addObject("year", year);
		mav.addObject("month", month);
		
		/**
		 * 학사안내 - 대학
		 */
		IndexInfoData indexInfoData = new IndexInfoData("http://www.skhu.ac.kr/calendar/calendar_list_1.aspx");
		mav.addObject("university", indexInfoData.getInfoValue());
		
		/**
		 * 학사안내 - 대학원
		 */
		indexInfoData.setUrl("http://www.skhu.ac.kr/calendar/calendar_list_2.aspx");
		mav.addObject("graduate", indexInfoData.getInfoValue());	
		
		/**
		 * 방명록 초기화면
		 */
//		String url = "http://m.web1234.woobi.co.kr/guest_book.php";
//		//해당 URL 페이지를 가져온다.
//		Source source = new Source(new URL(url));
//		//메소드 찾기를 위해 시작부터 끝까지 태그들만 parse 한다 (?)
//		source.fullSequentialParse();
//		
//		Element guestbook = source.getElementById("guest_book");
//		mav.addObject("guestbook", guestbook.toString());
		
		return mav;
	}
	
}
